using UnityEngine;
using System.Collections;
using Scenes = Enums.Scenes;

public class GamePlayMain : MonoBehaviour
{
    private Vector3 m_hitPoint;                                             // the location where on the terrain the tower should land
    public float distance = 250;                                            // distance at which a tower will start falling from
    private TowerManager m_towerMgr;
    private PathManager m_pathMgr;
    private ObjectFactory<string, Base> m_objFact;
    private AssetManager m_assetMgr;

    void Awake()
    {


    }
    // Use this for initialization
    void Start()
    {
        //init object factory for all objects
        m_objFact = ObjectFactory<string, Base>.Instance;
        m_objFact.Initalize();

        //init path manager
        m_pathMgr = PathManager.Instance;
        m_pathMgr.Initialize();
        m_pathMgr.CollectPathsFromHierarchy();


        //init tower manager
        m_towerMgr = TowerManager.Instance;
        m_towerMgr.Initialize();

        //switch scene test
        //SceneManager.ChangeScene(Scenes.MainMenu);

        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        sphere.AddComponent<Follower>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 a = new Vector3(Screen.width / 2, Screen.height / 2);
        Vector3 b = new Vector3();
        Debug.DrawLine(a, b, Color.red);
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                m_hitPoint = hit.point;

            }

        }
        if (m_hitPoint != Vector3.zero && Input.GetMouseButtonUp(0))
        {
            //todo: check if there isn't already a tower here
            //drop a tower, and added it to the towerManager
            GameObject newTower = GameObject.Instantiate(Resources.Load("Prefabs/tower")) as GameObject;
            newTower.transform.localPosition = m_hitPoint + (Vector3.up * distance);
            TowerManager.Instance.AddTower(Enums.TowerNames.LASER, newTower);
            m_hitPoint = Vector3.zero;
        }
    }

    void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(m_hitPoint, 10);
    }
}